package server.gameplay;

import java.nio.channels.SocketChannel;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

/**
 * @author Erik
 * A class that holds an ArrayList of ServerPlayerPackage(players) and functions to manipulate the players. 
 */
public class ServerPlayersPackage {
	private List<ServerPlayerPackage> players;
	
	public ServerPlayersPackage() {
		players = Collections.synchronizedList(new ArrayList<ServerPlayerPackage>());
	}
	
	public ServerPlayerPackage find(SocketChannel socket) {
		synchronized (players) {
			Iterator<ServerPlayerPackage> itr = players.iterator();
			while (itr.hasNext()) {
				ServerPlayerPackage p = itr.next();
				if (p.getSocket() == socket) {
					return p;
				}
			}
			// assert p != null;
			return null;
		}
	}

	public ServerPlayerPackage find(String nickName) {
		synchronized (players) {
			Iterator<ServerPlayerPackage> itr = players.iterator();
			while (itr.hasNext()) {
				ServerPlayerPackage p = itr.next();
				if (p.getNickName().equals(nickName)) {
					return p;
				}
			}
			// assert p != null;
			return null;
		}
	}
	
	public ServerPlayerPackage find(int id) {
		synchronized (players) {
			Iterator<ServerPlayerPackage> itr = players.iterator();
			while (itr.hasNext()) {
				ServerPlayerPackage p = itr.next();
				if (p.getID() == id) {
					return p;
				}
			}
			// assert p != null;
			return null;
		}
	}

	// set the first player in the list to admin, if no other admin is found
	/**
	 * Set the first player in the list to admin, if no other admin is found
	 */
	public void setAdmin() {
		synchronized (players) {
			// make sure there is no other admin
			Iterator<ServerPlayerPackage> itr = players.iterator();
			while (itr.hasNext()) {
				if (itr.next().isAdmin())
					return;
			}
			//set a new admin
			itr = players.iterator();
			while(itr.hasNext()){
				ServerPlayerPackage p = itr.next();
				if (p.getID() != ServerPlayerPackage.NEUTRAL_PLAYER) {
					p.setAdmin(true);
					//update the name..
					p.setNickName(p.getNickName());
					System.out.println(p.getNickName() + " is admin!");
					return;
				}
			}
		}
	}

	public void add(ServerPlayerPackage player) {
		assert player != null;
		players.add(player);
	}

	public void remove(ServerPlayerPackage player) {
		assert player != null;
		players.remove(player);
	}

	public void remove(SocketChannel socket) {
		players.remove(find(socket));
	}

	public Iterator<ServerPlayerPackage> iterator() {
		return players.iterator();
	}

	/**
	 * set the player's (with the SocketChannel socket) name to nickName
	 * @param socket
	 * @param nickName
	 */
	public void setNickName(SocketChannel socket, String nickName) {
		ServerPlayerPackage p = find(socket);
		assert p != null;
		p.setNickName(nickName);
	}

	//convert the players-array to a string
	public String toPackage() {
		StringBuffer buf = new StringBuffer();
		Iterator<ServerPlayerPackage> itr = players.iterator();
		
		while (itr.hasNext())
			buf.append(itr.next().toPackage());
		return buf.toString();
	}

	public List<ServerPlayerPackage> getPlayer() {
		return players;
	}
}
